<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
            </el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区 -->
        <el-card>
            <div>
                <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable
                    @clear="getGoodsList()">
                    <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
                </el-input>
                <el-button type="primary" style="margin-left: 15px;" @click="goAddPage">添加商品</el-button>
                <el-table :data="goodstableData" style="width: 100%" border stripe>
                    <el-table-column type="index" width="50">
                    </el-table-column>
                    <el-table-column prop="goods_name" label="商品名称">
                    </el-table-column>
                    <el-table-column prop="goods_price" label="商品价格(元)" width="112">
                    </el-table-column>
                    <el-table-column prop="goods_weight" label="商品重量(kg)" width="112">
                    </el-table-column>
                    <el-table-column label="创建时间" prop="add_time" width="170">
                        <template slot-scope="{row}">
                            {{addDate(row.add_time)}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="{row}">
                            <el-button type="primary" size="mini">编辑</el-button>
                            <el-button type="danger" size="mini" @click="deleteGoods(row.goods_id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- 分页器 -->
            <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" :current-page="this.queryInfo.pagenum"
                :page-sizes="[5, 10, 20, 100]" :page-size="100" layout="prev, pager, next, jumper,->,total, sizes"
                :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>
<script>
import * as dayjs from 'dayjs'
export default {
  data() {
    return {
      goodstableData: [],

      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
    }
  },

  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      let { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.goodstableData = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    addDate(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    searchGoods() {
      if (this.queryInfo.query.trim().length > 0) {
        this.getGoodsList()

        return
      }
    },
    // 删除商品
    async deleteGoods(row) {
      let resDelete = await this.$confirm(
        '此操作将永久删除, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (resDelete !== 'confirm') {
        return this.$message.error('已取消删除')
      }
      let { data: res } = await this.$http.delete(`goods/${row}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    goAddPage() {
      this.$router.push('/goods/add')
    },
  },
}
</script>
<style lang="less" scoped>
.input-with-select {
  width: 300px;
  margin-bottom: 20px;
}
</style>